package org.apak.xslt;

import java.io.File;
import java.io.PrintWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apak.log.FileLogger;
import org.apak.model.ServiceRequest;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XMLBuilder {
	
	public static void makeXML(ServiceRequest[] serviceRequestArray, String xsltFileName, PrintWriter out) {
		try {
			 
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
	 
			// root elements
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("requestList");
			doc.appendChild(rootElement);
			
			for(ServiceRequest request : serviceRequestArray) {
				String variable = "";
				
				Element customer = doc.createElement("customer");
				rootElement.appendChild(customer);
				
				Element id = doc.createElement("id");
				id.appendChild(doc.createTextNode(Integer.toString(request.getService_request())));
				customer.appendChild(id);
				
				Element customerName = doc.createElement("customerName");
				if(request.getCustomerName() != null) {
					variable = request.getCustomerName();
				} else {
					variable = "";
				}
				customerName.appendChild(doc.createTextNode(variable));
				customer.appendChild(customerName);
				
				Element createdBy = doc.createElement("createdBy");
				if(request.getCreated_byName() != null) {
					variable = request.getCreated_byName();
				} else {
					variable = "";
				}
				createdBy.appendChild(doc.createTextNode(variable));
				customer.appendChild(createdBy);
				
				Element customerDesc = doc.createElement("customerDesc");
				if(request.getService_desc_by_customer() != null) {
					variable = request.getService_desc_by_customer();
				} else {
					variable = "";
				}
				customerDesc.appendChild(doc.createTextNode(variable));
				customer.appendChild(customerDesc);
				
				Element employeeDesc = doc.createElement("employeeDesc");
				if(request.getService_desc_by_employee() != null) {
					variable = request.getService_desc_by_employee();
				} else {
					variable = "";
				}
				employeeDesc.appendChild(doc.createTextNode(variable));
				customer.appendChild(employeeDesc);
				
				Element status = doc.createElement("status");
				if(request.getService_request_status_typeName() != null) {
					variable = request.getService_request_status_typeName();
				} else {
					variable = "";
				}
				status.appendChild(doc.createTextNode(variable));
				customer.appendChild(status);
				
				Element serviceRequest = doc.createElement("serviceRequest");
				serviceRequest.appendChild(doc.createTextNode(Integer.toString(request.getService_request())));
				customer.appendChild(serviceRequest);
			}
			File xsltFile = new File(xsltFileName);
			Source xsltSource = new StreamSource(xsltFile);
			
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer(xsltSource);
			DOMSource xmlSource = new DOMSource(doc);	

			transformer.transform(xmlSource, new StreamResult(out));
	 
		  } catch (ParserConfigurationException pce) {
			  FileLogger.log("XMLBuilder.makeXML()", FileLogger.getStackTrace(pce));
		  } catch (TransformerException tfe) {
			  FileLogger.log("XMLBuilder.makeXML()", FileLogger.getStackTrace(tfe));
		  }
	}

}
